using Microsoft.AspNetCore.Mvc;
using MsgBackend.Api.Repository;
using MsgBackend.Api.Models;
using Microsoft.Extensions.Configuration;
using MsgBackend.Api.ParamModel;
using System.Linq;
using System;
namespace MsgBackend.Api.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class ArticleController : ControllerBase
    {
        private IRepository<Articles> _ArticlesRepository;
        private IRepository<Admins> _AdminsRepository;
        private IRepository<ArticlesPicture> _ArticlesPicture;
        private IRepository<ArticleStatus> _ArticleStatus;
        private IRepository<ArticleType> _ArticleType;
        private readonly IConfiguration _configuration;
        public ArticleController(IConfiguration configuration,
         IRepository<Articles> articlesrepository,
         IRepository<Admins> AdminsRepository,
         IRepository<ArticlesPicture> ArticlesPicture,
         IRepository<ArticleStatus> ArticleStatus,
         IRepository<ArticleType> ArticleType)
        {
            _ArticlesRepository = articlesrepository;
            _configuration = configuration;
            _AdminsRepository=AdminsRepository;
            _ArticlesPicture=ArticlesPicture;
            _ArticleStatus=ArticleStatus;
            _ArticleType=ArticleType;
        }
        //获取所有书籍
        [HttpGet("Articles")]
        public ActionResult GetArticles()
        {
            var article = _ArticlesRepository.Table;
            return Ok(new
            {
                Code = 1000,
                Data = article,
                Msg = "获得成功"
            });

        }

        //添加新的文章
        [HttpPost,Route("AddArticle")]
        public ActionResult AddArticle(ParamArticle model)
        {

            var article = new Articles
            {
                ArticleName = model.ArticleName,
                ArticleTitle = model.ArticleTitle,
                ArticleAbstract = model.ArticleAbstract,
                ArticleContent = model.ArticleContent,
                IsRecommend=model.IsRecommend,
                Admins=_AdminsRepository.Table.Single(r=>r.Id==1),
                ArticleStatus=_ArticleStatus.Table.Single(r=>r.Id==1),
                ArticlesPicture=_ArticlesPicture.Table.Single(r=>r.Id==1),
                ArticleType=_ArticleType.Table.Single(r=>r.Id==1)
                // ArticleTypeId=model.ArticleTypeId,
                // AdminsId=model.AdminsId,
                // ArticlesPictureId=model.ArticlesPictureId,
                // ArticleStatusId=model.ArticleStatusId
            };
       
            _ArticlesRepository.Insert(article);
            return Ok(new 
            {
                Code = 1000,
                Data =new ReturnArticles{
                ArticleName = model.ArticleName,
                ArticleTitle = model.ArticleTitle,
                ArticleAbstract = model.ArticleAbstract,
                ArticleContent = model.ArticleContent,
                IsRecommend=model.IsRecommend
                },
                Msg = "添加成功"
            });
        }
        //根据文章名查询
        [HttpPost("ArticleName"), ]

        public ActionResult ArticleName(ParamArticle model)
        {
             var articles = _ArticlesRepository.Table;

            var article = articles.Where(x => x.ArticleName.Contains(model.ArticleName));
            if (article == null)
            {
                return Ok(new
                {
                    Code = 1002,
                    Data = " ",
                    Msg = "未查询到"
                });
            }
            else
            {
                return Ok(new
                {
                    Code = 1000,
                    Data = article,
                    Msg = "获得成功"
                });
            }


        }
        //删除文章
        [HttpDelete("{id}")]
        public ActionResult DeleteArticle(int id)
        {
            var article = _ArticlesRepository.GetById(id);
            if (article != null)
            {
                article.IsActived = false;
                article.IsDeleted = true;
                article.UpdatedTime = DateTime.Now;
                _ArticlesRepository.Delete(article);
                return Ok(new
                {
                    Code = 1000,
                    Data = article,
                    Msg = "删除成功"
                });
            }
            else
            {
                return Ok(new
                {
                    Code = 1001,
                    Data = " ",
                    Msg = "删除失败"
                });
            }

        }
        //修改文章内容
        [HttpPut("{id}")]
        public ActionResult UpdateArticle(ParamArticle model, int id)
        {
            var articles = _ArticlesRepository.GetById(id);
            if (articles != null)
            {
              
                    articles.ArticleName = model.ArticleName;
                    articles.ArticleTitle = model.ArticleTitle;
                    articles.ArticleAbstract = model.ArticleAbstract;
                    articles.ArticleContent = model.ArticleContent;
                    articles.IsRecommend=model.IsRecommend;
                _ArticlesRepository.Update(articles);
                return Ok(new
                {
                    Code = 1000,
                    Data = articles,
                    Msg = "修改成功"
                });
            }
            return Ok(new
            {
                Code = 1002,
                Data = "",
                Msg = "修改失败"
            });

        }
    }
}